﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.固定资产
{
    public partial class Form固定资产创建 : DevExpress.XtraEditors.XtraForm
    {
        private AssetsDTO assetsDTO = new AssetsDTO();
        private Employee employee = new Employee();
        public Form固定资产创建(String assertNo)
        {
            InitializeComponent();
            this.assetsDTO.assertNo = assertNo;
            this.textBox资产卡片编号.Text = assertNo;
            this.searchLookUpEdit供应商.Properties.DataSource = 固定资产Service.getAssertVendorDTOs();
            this.searchLookUpEdit类别.Properties.DataSource = 固定资产Service.getAssertTypeDTO();
        }

        #region 选择供应商
        private void searchLookUpEdit供应商_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit供应商View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit供应商.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.assetsDTO.vendorId = this.searchLookUpEdit供应商.Properties.View.GetRowCellValue(rowHandle, "vendorId").ToString();
                    this.assetsDTO.vendorName = this.searchLookUpEdit供应商.Properties.View.GetRowCellValue(rowHandle, "vendorName").ToString();
                    this.assetsDTO.contract = this.searchLookUpEdit供应商.Properties.View.GetRowCellValue(rowHandle, "contract").ToString();
                    this.assetsDTO.mobile = this.searchLookUpEdit供应商.Properties.View.GetRowCellValue(rowHandle, "mobile").ToString();
                    this.assetsDTO.email = this.searchLookUpEdit供应商.Properties.View.GetRowCellValue(rowHandle, "email").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit供应商.ToolTip = null;
            searchLookUpEdit供应商.EditValue = null;
        }
        private void searchLookUpEdit供应商_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.assetsDTO.vendorName;
        }
        #endregion

        #region 选择资产类别
        private void searchLookUpEdit类别_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn类别_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit类别View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit类别.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.assetsDTO.assertTypeId = this.searchLookUpEdit类别.Properties.View.GetRowCellValue(rowHandle, "vendorId").ToString();
                    this.assetsDTO.name = this.searchLookUpEdit类别.Properties.View.GetRowCellValue(rowHandle, "vendorName").ToString();
                    this.assetsDTO.assertType = this.searchLookUpEdit类别.Properties.View.GetRowCellValue(rowHandle, "contract").ToString();
                    this.assetsDTO.band = this.searchLookUpEdit类别.Properties.View.GetRowCellValue(rowHandle, "mobile").ToString();
                    this.assetsDTO.usage = this.searchLookUpEdit类别.Properties.View.GetRowCellValue(rowHandle, "email").ToString();
                    this.assetsDTO.docUrl = this.searchLookUpEdit类别.Properties.View.GetRowCellValue(rowHandle, "docUrl").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn类别_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit类别.ToolTip = null;
            searchLookUpEdit类别.EditValue = null;
        }
        private void searchLookUpEdit类别_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.assetsDTO.assertType;
        }
        #endregion
        private void textBox归属_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                this.employee = 固定资产Service.getEmployeeShortDTOByMnemonic(this.textBox归属.Text.Trim());
            }
        }

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            if (null == this.employee)
            {
                MessageBox.Show("请在资产归属扫描正确的员工牌");
                this.ActiveControl = this.textBox归属;
                return;
            }
            this.assetsDTO.ownerShipId = this.employee.id;
            this.assetsDTO.ownerShip = this.employee.name;
            this.assetsDTO.location = this.textBox位置.Text;
            this.assetsDTO.inUse = this.chekBox在用.Checked;
            this.assetsDTO.discard = this.chekBox在用.Checked;
            this.assetsDTO.createDate = DateTime.Now;
            if (!String.IsNullOrEmpty(this.assetsDTO.assertNo))
            {
                Boolean ifCreate = 固定资产Service.createAssetsDTO(this.assetsDTO);
                if (ifCreate)
                {
                    MessageBox.Show("创建成功");
                }
                else
                {
                    MessageBox.Show("创建失败");
                    return;
                }
            }
        }

        private void simpleButton新增供应商_Click(object sender, EventArgs e)
        {
            new Form供应商创建().ShowDialog();
            this.searchLookUpEdit供应商.Properties.DataSource = 固定资产Service.getAssertVendorDTOs();
            this.searchLookUpEdit供应商.Refresh(); 
        }

        private void simpleButton新增资产类别_Click(object sender, EventArgs e)
        {
            new Form资产类别创建().ShowDialog();
            this.searchLookUpEdit类别.Properties.DataSource = 固定资产Service.getAssertTypeDTO();
            this.searchLookUpEdit类别.Refresh();
        }

    }
}
